// pages/activity/gather/index.js
const app = getApp();
const CryptoJS = require('../../../utils/aes/crypto-js');
let config = require("../../../utils/config");
import {http} from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false, //暂时屏蔽二维码
    isError: false, //是否显示404
    isReceive: false, //是否已领取
    isFirst: false, //是否第一次领取

    expandInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    if(config.OPENID()){
      that.queryActivityInfo();
      return;
    }
    app.initCallBack = res=>{
      that.queryActivityInfo();
    }
  },

   /** 活动信息 */
   queryActivityInfo(){
     let that = this;
    http.requestData("GET","ExpandMem/GetExpandInfo",{
      appletId: config.OPENID()
    }).then(res=>{
      if(res.Tag == 10){
        res.Data.BgPic = config.hostUrl + res.Data.BgPic;
        that.setData({
          expandInfo: res.Data,
          isReceive: res.Extend.isReceive,
          isError: res.Data.Status==10?false:true
        })
        wx.setNavigationBarTitle({
          title: res.Data.Title,
        })
      }else{
        http.showToast(res.Message);
        that.setData({
          isError: true
        })
        wx.setNavigationBarTitle({
          title: "好礼送不停",
        })
      }
    }).catch(err=>{
      http.showToast("服务繁忙，请稍后重试")
      that.setData({
        isError: true
      })
      wx.setNavigationBarTitle({
        title: "好礼送不停",
      })
    })
  },

  /** 领取 */
  getTap(){
    let that = this;
    if(that.data.isReceive){
      return;
    }
    http.requestData("POST","ExpandMem/Save",{
      appletId: CryptoJS.Encrypt("liveopid" + config.OPENID()),
      giftGuid: this.data.expandInfo.Guid,
      ModifyName: config.OPENID()
    }).then(res=>{
      if(res.Tag==10){
        http.showToast("领取成功");
        that.setData({
          isFirst: true,
          isReceive: true
        })
      }else{
        http.showToast(res.Message);
      }
    }).catch(err=>{})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})